/*
 * Copyright 2008 the original author Ang Gao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * YahooSearcher.java 2008-11-11
 */
package ie.ucc.cs.anggao.finalproject.client.support.searchengine;

import ie.ucc.cs.anggao.finalproject.client.model.SearchEngine;
import ie.ucc.cs.anggao.finalproject.client.model.SearchEngineResult;
import ie.ucc.cs.anggao.finalproject.client.support.async.JSONRequest;
import ie.ucc.cs.anggao.finalproject.client.support.async.JSONRequestHandler;
import ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.infopane.InfoPane;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;

/**
 * @author GaoAng
 * 
 */
public class YahooSearcher extends BaseSearcher implements JSONRequestHandler {

	public YahooSearcher(InfoPane view) {
		super(view, new SearchEngine("Yahoo Search", "http://www.yahoo.com"));
	}

	public void doIR(String q) {
		
		JSONRequest.get( "http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=J6rO9izV34EhxGtDMl4W5Dzj0Hs33LosiJoB7SAtdbVtTn7djfGD5sdOPF5TXMLD&query="+q+"&output=json&callback=", this  );
		//JSONRequest.get( "http://boss.yahooapis.com/ysearch/web/v1/UCC?appid=v7zvQO3V34EOC7zHEP5t9Ii7cGUodHF_300VlYCINop0MEkJQlSIBcGXvgit0BtwX5uPtmKcIw--&format=json&callback=", this  );
		getView().clearResults();
		
	}

	public void onRequestComplete( JavaScriptObject json ){
//		JSONObject j = new JSONObject(json);
//		JSONObject root = (JSONObject)j.get("ysearchresponse ");
//		JSONObject feed = (JSONObject)root.get("resultset_web ");
//		JSONArray entries = (JSONArray)feed.get("result");
		
		JSONObject j = new JSONObject(json);
		JSONObject feed = (JSONObject)j.get("ResultSet");
		JSONArray entries = (JSONArray)feed.get("Result");
		
		for( int i=0; i< entries.size(); ++i ){
			JSONObject entry = (JSONObject)entries.get(i);			

//			JSONString title = (JSONString)entry.get("title");
//			JSONString url = (JSONString)entry.get("url");
//			JSONString summary = (JSONString)entry.get("Summary");
			
			
			JSONString title = (JSONString)entry.get("Title");
			JSONString url = (JSONString)entry.get("Url");
			JSONString summary = (JSONString)entry.get("Summary");
			if( url != null ){
				getView().addSearchResult(
						new SearchEngineResult( title.stringValue(),url.stringValue(), summary.stringValue() ) );
			}
		}
	}

}
